--[[
 * Copyright (c) 2010 MoSync AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
--]]

-- EXPERIMENTAL - WORK IN PROGRESS

----------------------------------------------------------------------
-- This file contains functions that enables downloading code files --
----------------------------------------------------------------------

-- LoadCode loads code files from one or more urls, and
-- then calls a success function or an error function.

function LoadCode(...)
  
  local successFun = arg[arg.n - 1]
  local errorFun = arg[arg.n]
  local numberOfActiveDownloads = arg.n - 2
  local success = true -- We are optimistic!
  
  local function handleDownloadFun(data)
    if nil ~= data then
      assert(loadstring(data))()
    else
      success = false
    end
    numberOfActiveDownloads = numberOfActiveDownloads - 1
    if 0 == numberOfActiveDownloads then
      if success then
        successFun()
      else
        errorFun()
      end
    end
  end
  
  for i,value in ipairs(arg) do
    if i < arg.n - 1 then
      LoadTextFile(value, handleDownloadFun)
    end
  end

end

--[[
-- Example program

LoadCode(
  'http://comikit.se/mosync/Lib.lua',
  function() Foo() end,
  function() Log('Failed to load Lib.lua') end)

--]]

-- LoadLib loads code from one or more urls and then calls 
-- OnInit and OnDraw if successful. Event handling is paused 
-- during download.

function LoadLib(...)
  PauseEventHandling()
  arg[arg.n + 1] = function()
    ResumeEventHandling()
    if nil ~= OnInit then OnInit() end
    if nil ~= OnDraw then OnDraw() end
  end
  arg[arg.n + 2] = function()
    Log('Error loading libs')
  end
  LoadCode(unpack(arg))
end

--[[ 
-- Example program
LoadLib(
  'http://comikit.se/mosync/Lib.lua',
  'http://comikit.se/mosync/Lib2.lua',
  'http://comikit.se/mosync/Lib3.lua')

function OnInit()
  FooBar() -- Defined in Lib3.lua
  Foo() -- Defined in Lib1.lua
  Bar() -- Defined in Lib2.lua
end

-- This example does not use OnDraw
--]]

-----------------------------------------------------------------------------
--------------------------- Load main application ---------------------------
-----------------------------------------------------------------------------

-- Here we start the program by running the code in App.lua
-- That file contains a Main function that sets things off.
-- Adapt this example to suit your own needs.

-- Turn event processing off during loading.
PauseEventHandling()

-- Load the code.
LoadCode(
  'http://comikit.se/mosync/App.lua', 
  function() Main() end, 
  function() Log('Error loading App.lua') end)
